package com.geektime.hello;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author DRevelation
 * @Date 2020-07-31 下午 12:08
 * @Desc
 */
public class UploadDemo {

    public static void main(String[] args) throws Exception {
        String htmlStr = "<img style=\"max-width:100%;\" src=\"http://ahtvoss.oss-cn-heyuan.aliyuncs.com/20200731122621/095a7b9c7f9b4518beef2249aad89c80.jpg\" />121212";
        String htmlStr2 = "7月29日，<img src=\"https://t7.baidu.com/it/u=3616242789,1098670747&fm=79&app=86&size=h300&n=0&g=4n&f=jpeg?sec=1596783247&t=3f5809971db3c85d5830842619f70077\" />";
        String replaceHtml = UploadDemo.getImgSrc(htmlStr2, "article");
        System.out.println(replaceHtml);
        // System.out.println(UploadDemo.getImgStr2(htmlStr2));
    }

    public static String getImgSrc(String htmlStr, String path) throws Exception {
        //匹配字符串中的img标签
        Pattern p = Pattern.compile("<(img|IMG)(.*?)(>|></img>|/>)");
        Matcher matcher = p.matcher(htmlStr);

        //判断是否含有图片 //如果含有图片，那么持续进行查找，直到匹配不到
        while (matcher.find()) {
            String group = matcher.group();
            if (group.contains("data-src") == true) {
                String src = group.substring(group.lastIndexOf("src"));
                //匹配图片的地址
                Pattern srcText = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                //匹配图片的地址
                Pattern srcText2 = Pattern.compile("(data-src|DATA-SRC)=(\"|\')(.*?)(\"|\')");
                Matcher matcher2 = srcText2.matcher(group);
                Matcher matcher3 = srcText.matcher(src);
                //判断是否为域名，不是域名则下载到服务器
                Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
                if (matcher2.find() == true && pattern.matcher(matcher2.group()).matches() == false && matcher3.find() == true) {
                    URL url = new URL(matcher2.group().substring(10, matcher2.group().length() - 1));
                    URL url1 = new URL(matcher3.group().substring(5, matcher3.group().length() - 1));

                    //替换图片url
                    String replaceHttp = uploadToOSS(url, path);
                    String countx = htmlStr.replace(String.valueOf(url1), replaceHttp);
                    htmlStr = countx;
                }
            } else {
                //匹配图片的地址
                Pattern srcText = Pattern.compile("(src|SRC)=(\"|\')(.*?)(\"|\')");
                Matcher matcher2 = srcText.matcher(group);
                Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
                if (matcher2.find() == true && pattern.matcher(matcher2.group()).matches() == false) {
                    URL url = new URL(matcher2.group().substring(5, matcher2.group().length() - 1));

                    //替换图片url
                    String replaceHttp = uploadToOSS(url, path);
                    String countx = htmlStr.replace(String.valueOf(url), replaceHttp);
                    htmlStr = countx;
                }
            }
        }
        return htmlStr;
    }

    // public static String getImgStr(String htmlStr, String path) {
    //     String replaceHtml = htmlStr;
    //     String img = "";
    //     Pattern p_image;
    //     Matcher m_image;
    //     // String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
    //     String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
    //     p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
    //     m_image = p_image.matcher(htmlStr);
    //     while (m_image.find()) {
    //         // 得到<img />数据
    //         img = m_image.group();
    //         // 匹配<img>中的src数据
    //         Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
    //         while (m.find()) {
    //             String url = m.group(1);
    //             String replaceUrl = uploadToOSS(url, path);
    //             replaceHtml = htmlStr.replace(url, replaceUrl);
    //         }
    //     }
    //     return replaceHtml;
    // }

    private static String uploadToOSS(URL fileUrl, String path) {
        String articleImgUrl = "aaa-bbb" + fileUrl;
        return articleImgUrl;
    }

    /**
     * 得到网页中图片的地址
     */
    public static Set<String> getImgStr2(String htmlStr) {
        Set<String> pics = new HashSet<String>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
// String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
// 得到<img />数据
            img = m_image.group();
// 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }

}
